package com.dragon.gencode.exception;

import com.dragon.gencode.project.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 拦截所有运行时的全局异常   
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public AjaxResult runtimeException(RuntimeException e) {
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getLocalizedMessage());
    }

    /**
     * 系统异常捕获处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult exception(Exception e) {
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getLocalizedMessage());
    }
}
